﻿using B.S.DataBase.MyProject.Domain.HealthMonitoring;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Write.API.Appcation.Commands.HealthMonitoringCommand;

namespace MedicalPCTerminal.Write.API.Appcation.CommandHandlers.HealthMonitoringCommandHandler
{
    /// <summary>
    /// 修改血糖
    /// </summary>
    public class UpdateBloodSugarCommandHandler : IRequestHandler<UpdateBloodSugarCommand, APIResult<int>>
    {
        private readonly SqlSugarContext sqlSugar;

        public UpdateBloodSugarCommandHandler(SqlSugarContext sqlSugar)
        {
            this.sqlSugar = sqlSugar;
        }
        public async Task<APIResult<int>> Handle(UpdateBloodSugarCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<int> result = new APIResult<int>();
                var list = sqlSugar.db.Queryable<BloodSugar>().Where(x => x.SId == request.SId).First();
                list.FastingBloodGlucose = request.FastingBloodGlucose;
                list.PostprandialBloodGlucose = request.PostprandialBloodGlucose;
                var res = sqlSugar.db.Updateable(list).ExecuteCommand();
                if (res > 0)
                {
                    result.Code = APIEnum.修改成功;
                    result.Msg = APIEnum.修改成功.ToString();
                    result.data = res;
                }
                else
                {
                    result.Code = APIEnum.修改失败;
                    result.Msg = APIEnum.修改失败.ToString();
                    result.data = res;
                }

                return await Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
